package com.huwei.boxuegu.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.huwei.boxuegu.R;
import com.huwei.boxuegu.activity.CourseDetailActivity;
import com.huwei.boxuegu.activity.CourseModel;
import com.huwei.boxuegu.activity.LoginActivity;
import com.huwei.boxuegu.activity.LoginModel;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/4/2.
 */

public class CourseFragment extends Fragment {
    RecyclerView mRecycleView;
    public ArrayList<CourseModel.Course> courselists = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_course,container,false);
        mRecycleView = view.findViewById(R.id.recycleview);
        loading();
        return view;
    }

    public void loading(){
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.Course.Getcourse");//请求的域名
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson  =new Gson();
                CourseModel courseModel = gson.fromJson(result,CourseModel.class);
                if (courseModel!= null&&courseModel.data!= null && courseModel.data.courselist!=null) {
                    courselists = courseModel.data.courselist;//todo 判空
                }
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),2);
                mRecycleView.setLayoutManager(gridLayoutManager);

                mRecycleView.setAdapter(new MyAdatper());
                Log.i("LoginActivity","result:" +result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    //定义adpter
    public class  MyAdatper extends RecyclerView.Adapter<MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(),R.layout.item_course,null);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final CourseModel.Course  course = courselists.get(position);
            holder.titleTv.setText(course.title);
            holder.contentTitleTv.setText(course.titlecontent);
            x.image().bind(holder.imageView,course.icon);//图片请求
            holder.mRootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), CourseDetailActivity.class);

                    intent.putExtra("imaurl",course.imgurl);
                    intent.putExtra("des",course.des);
                    intent.putExtra("video",course.videourl);

                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return courselists.size();
        }
    }


    public class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView titleTv;
        TextView contentTitleTv;
        LinearLayout mRootView;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img);
            titleTv = itemView.findViewById(R.id.title);
            contentTitleTv = itemView.findViewById(R.id.contenttitle);
            mRootView = itemView.findViewById(R.id.rootview);
        }
    }

}
